class Solution:
	# @param num, a list of integer
	# @return a list of integer
	def nextPermutation(self, num):
		i = len(num)-2
		for x in xrange(len(num)-1):
			if num[i] < num[i+1]:
				break
			i -= 1
		if i < 0:
			return sorted(num)
		num[i+1:] = num[len(num)-1:i:-1]
		for x in xrange(i+1,len(num)):
			if num[x] > num[i]:
				t = num[i]
				num[i] = num[x]
				num[x] = t
				break
		return num

if __name__ == '__main__':
	so = Solution()
	a = [5,4,3]
	for i in xrange(10):
		a = so.nextPermutation(a)
		print a